package GUI;

import order.*;
import model.*;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import GUI.MapPanel.TerritoryPanel;

public class MoveSpyFrame extends OrderFrame{
	 private JComboBox<TechnologyLevel> spyBox;
	 private JComboBox<Integer> destinationBox; 
     private MoveSpy temp;

	 public MoveSpyFrame(TextPanel textPanel, TerritoryPanel territoryPanel, Map map){
		super(textPanel, territoryPanel, map);
			
		this.setLayout(new GridLayout(4, 2, 10, 10));	   
		
	  	this.add(new JLabel("Move spy: ")); 
	 	  	
	  	spyBox = new JComboBox<TechnologyLevel>();
	  	for (Spy spy: player.getSpyList()){
	  	   if ((spy.getTerritory() == territory) && (!spy.getActioned())) 
	  		   spyBox.addItem(spy.getLevel());
	  	}
	  	this.add(spyBox);
	  	this.add(new JLabel("at "));
	  	this.add(new JLabel(String.valueOf(territory.getLocation())));
	  	this.add(new JLabel("To Destiantion: "));
	  	destinationBox = new JComboBox<Integer>();
	  	for (Territory t : territory.getNeighbors()) //&& t.getOwner != default
	  	   destinationBox.addItem(t.getLocation());
	  	this.add(destinationBox);
	  	this.add(submitButton);
	  	this.add(cancelButton);
	  	submitButton.addActionListener(new submitListener(this));
	  	cancelButton.addActionListener(new cancelListener(this));
	  	
	  	this.setSize(400, 200);
	  	this.setVisible(true);
	 }
	 
	 class submitListener implements ActionListener{
   
		JFrame frame;
	  public submitListener(JFrame _frame){
			frame = _frame;
		}
	  public void actionPerformed(ActionEvent arg0) {
		Territory destination = map.getTerritory((int)destinationBox.getSelectedItem());
		TechnologyLevel spylevel = (TechnologyLevel)spyBox.getSelectedItem();
		HashMap<TechnologyLevel, Integer> trooplist = new HashMap<TechnologyLevel, Integer>();
		trooplist.put(spylevel, 1);
		
	  	 temp = new MoveSpy(territory, player, destination, trooplist);
	  	 if (temp.checkOrder(map)){
		  	    textPanel.addHistory(temp);
		  	    temp.executeOrder();
		  	    textPanel.updatePlayerPanel();
		  	  territoryPanel.update();
		  	  territoryPanel.getMapPanel().getTerritoryPanel(destination).update();
		  }
	  	 frame.dispose();
	}
	 }
	 
}
